package com.cc.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cc.entity.Answer;
import com.cc.entity.UserStudent;
import com.cc.mongo.Questions;
import com.cc.service.FnService;
import com.cc.service.QuestionsService;

@Controller  
@RequestMapping("/questions") 
public class QuestionsController {
	@Resource  
	protected FnService fnService;
	@Resource  
	protected QuestionsService questionService;
	
	@ResponseBody
	@RequestMapping("/private/list")  
    public String list(HttpServletRequest request,Model model,UserStudent userStudent){  
       UserStudent token=(UserStudent)request.getSession().getAttribute("token");
       UserStudent currentUser=fnService.getCurrentUserStudent(token);
       String questionidsstr=currentUser.getQuestionsids();
       if(questionidsstr==null||"".equals(questionidsstr.trim()))
    	   return "";
       List<String> questionsidslist=Arrays.asList(questionidsstr.trim().split(","));
       List<Questions> record=questionService.queryByidList(questionsidslist);
       ObjectMapper jsonmapper=new ObjectMapper(); 
	   String result="";
		try {
			result=jsonmapper.writeValueAsString(record);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;   
    }
	
	
	
	@ResponseBody
	@RequestMapping("/private/answer")  
    public String answer(HttpServletRequest request,Model model,@RequestBody Answer answer){ 
		int state=questionService.handelAnswer(answer);
		return state+"";
	}

}
